﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class SpecialNewsList : YuanlinUserControl,
        IReplyConverter<SpecialNewsReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList newss;
        private int provinceId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProvinceId);
        private int cityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CityId);
        private int townId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.TownId);
        private int smallTownId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.SmallTownId);
        private string queryText = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
        private int categroyType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                SpecialNewsListRequest request = new SpecialNewsListRequest();
                request.Province = provinceId;
                request.City = cityId;
                request.Town = townId;
                request.SmallTown = smallTownId;
                request.Title = queryText;
                request.CategoryType = categroyType;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                requestBinders.Add(RequestHelper.BuildGetSpecialNewsListRequest(this, request));
            }
        }

        private void BindData()
        {
            list.DataSource = newss;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(newss.TotalCount);
                currentPage.TotalRecords = (newss.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region IReplyConverter<SpecialNewsReply,GetListOperation> Members

        public void ConvertReply(SpecialNewsReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                newss = reply.EntityList;

                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
